package tech.aiotcloud.city.citybigdataweb.util.jsbridge;

import android.graphics.Bitmap;
import android.os.Build;
import android.webkit.GeolocationPermissions;
import android.webkit.WebResourceRequest;
import android.webkit.WebResourceResponse;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import androidx.annotation.Nullable;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;

/**
 * Created by bruce on 10/28/15.
 */
public class BridgeWebViewClient extends WebViewClient {
    private BridgeWebView webView;
    private WebViewAssetsIntercepter webViewAssetsIntercepter;

    public BridgeWebViewClient(BridgeWebView webView, WebViewAssetsIntercepter webViewAssetsIntercepter) {
        this.webView = webView;
        this.webViewAssetsIntercepter = webViewAssetsIntercepter;
    }




//    @Override
//    public void onProgressChanged(WebView view, int newProgress) {
//        super.onProgressChanged(view, newProgress);
//        if (newProgress == 100) {
//            webSettings.setBlockNetworkImage(true);
//        } else {
//        }
//    }
//
//    @Override
//    public void onReceivedIcon(WebView view, Bitmap icon) {
//        super.onReceivedIcon(view, icon);
//    }
//
//    @Override
//    public void onGeolocationPermissionsShowPrompt(String origin, GeolocationPermissions.Callback callback) {
//        callback.invoke(origin, true, false);
//        super.onGeolocationPermissionsShowPrompt(origin, callback);
//    }

        @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        try {
            url = URLDecoder.decode(url, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        if (url.startsWith(BridgeUtil.YY_RETURN_DATA)) { // 如果是返回数据
            webView.handlerReturnData(url);
            return true;
        } else if (url.startsWith(BridgeUtil.YY_OVERRIDE_SCHEMA)) { //
            webView.flushMessageQueue();
            return true;
        } else {
            return super.shouldOverrideUrlLoading(view, url);
        }
    }

    @Override
    public void onPageStarted(WebView view, String url, Bitmap favicon) {
        super.onPageStarted(view, url, favicon);
    }


    @Override
    public void onPageFinished(WebView view, String url) {
        super.onPageFinished(view, url);

        if (BridgeWebView.toLoadJs != null) {
            BridgeUtil.webViewLoadLocalJs(view, BridgeWebView.toLoadJs);
        }
        if (webView.getStartupMessage() != null) {
            for (Message m : webView.getStartupMessage()) {
                webView.dispatchMessage(m);
            }
            webView.setStartupMessage(null);
        }
    }


    @Nullable
    @Override
    public WebResourceResponse shouldInterceptRequest(WebView view, WebResourceRequest request) {
        WebResourceResponse intercepted = null;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            intercepted = webViewAssetsIntercepter.getAssetLoader().shouldInterceptRequest(request.getUrl());
            if (request.getUrl().toString().endsWith("js")) {
                if (intercepted != null) {
                    intercepted.setMimeType("text/javascript");
                }
            }
        }
        return intercepted;
    }


    @Override
    public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
        super.onReceivedError(view, errorCode, description, failingUrl);
    }
}